<template>
  <el-container class="app-container">
    <el-header>
      <div class="header-content">
        <div class="title">学生管理系统</div>
        <el-menu
          mode="horizontal"
          :router="true"
          class="menu"
          v-if="isLoggedIn"
        >
          <el-menu-item index="/">首页</el-menu-item>
          <el-menu-item index="/students">学生列表</el-menu-item>
        </el-menu>
        <div class="user-info" v-if="isLoggedIn">
          <el-dropdown @command="handleCommand">
            <span class="user-dropdown">
              {{ user?.name || user?.username }}
              <el-icon><arrow-down /></el-icon>
            </span>
            <template #dropdown>
              <el-dropdown-menu>
                <el-dropdown-item command="logout">退出登录</el-dropdown-item>
              </el-dropdown-menu>
            </template>
          </el-dropdown>
        </div>
      </div>
    </el-header>
    <el-main>
      <router-view></router-view>
    </el-main>
  </el-container>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import { ElMessage } from 'element-plus'
import { ArrowDown } from '@element-plus/icons-vue'

const router = useRouter()
const user = ref(null)

const isLoggedIn = computed(() => {
  return !!localStorage.getItem('token')
})

onMounted(() => {
  const userStr = localStorage.getItem('user')
  if (userStr) {
    user.value = JSON.parse(userStr)
  }
})

const handleCommand = (command) => {
  if (command === 'logout') {
    localStorage.removeItem('token')
    localStorage.removeItem('user')
    user.value = null
    router.push('/login')
    ElMessage.success('退出成功')
  }
}
</script>

<style scoped>
.app-container {
  min-height: 100vh;
}

.el-header {
  background-color: #409EFF;
  color: white;
  padding: 0;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
}

.header-content {
  max-width: 1200px;
  margin: 0 auto;
  height: 60px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
}

.title {
  font-size: 24px;
  font-weight: bold;
  white-space: nowrap;
  margin-right: 40px;
}

.menu {
  flex: 1;
  background: transparent;
  border: none;
}

.menu :deep(.el-menu-item) {
  color: white;
  font-size: 16px;
}

.menu :deep(.el-menu-item.is-active) {
  color: #ffd04b;
  border-bottom-color: #ffd04b;
}

.menu :deep(.el-menu-item:hover) {
  background-color: rgba(255, 255, 255, 0.1);
}

.user-info {
  margin-left: 20px;
}

.user-dropdown {
  color: white;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 4px;
}

.el-main {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
}
</style> 